package com.tuo51.evaluate.controller;

import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.domain.childrens.Children;
import com.tuo51.evaluate.service.UploadService;
import com.tuo51.foudation.constant.RequestConstant;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

import static com.tuo51.foudation.model.ErrorCode.FILE_MUST_NOT_NULL;


@RestController
@RequestMapping("/eapi/upload")
@Api(value = "/eapi/upload", tags = "测评 - 上传")
public class UploadController {

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private StaffService staffService;

    @Autowired
    private UploadService uploadService;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;

    @RequestMapping(value = "/children_head_img" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse uploadHeadImg(@RequestHeader(RequestConstant.TICKET)String ticket, String childrenId, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MUST_NOT_NULL,"文件不能为空");

        String staffId = cache.getStaffId(ticket);

        Children children = uploadService.uploadChildrenHeadImg(childrenId, file);
        return StatusResponse.success(children);
    }

    @RequestMapping(value = "/teacher_head_img" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse teacherUploadImg(@RequestHeader(RequestConstant.TICKET)String ticket, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MUST_NOT_NULL,"文件不能为空");

        String staffId = cache.getStaffId(ticket);

        String url = uploadService.uploadTeacherHeadImg(ticket, file);
        return StatusResponse.success(url);
    }

    @RequestMapping(value = "/area_logo" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse areaUploadLogo(@RequestHeader(RequestConstant.TICKET)String ticket, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MUST_NOT_NULL,"文件不能为空");

        String staffId = cache.getStaffId(ticket);


        String url = uploadService.uploadAreaLogo(staffId, file);

        staffService.modifyAreaLogo(staffId, url);

        return StatusResponse.success(url);
    }
}
